home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
mint
/
editors
/
mntemacs.zoo
/
src
/
scroll.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-02
|
16KB
|
508 lines
/* Calculate what ins/del line to do, and do it, for Emacs redisplay.
Copyright (C) 1985, 1986, 1990 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include "config.h"
#include "termchar.h"
#include "termhooks.h"
#include "dispextern.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
struct matrix_elt
{
/* Cost of outputting through this line
if no insert/delete is done just above it. */
int writecost;
/* Cost of outputting through this line
if an insert is done just above it. */
int insertcost;
/* Cost of outputting through this line
if a delete is done just above it. */
int deletecost;
/* Number of inserts so far in this run of inserts,
for the cost in insertcost. */
char insertcount;
/* Number of deletes so far in this run of deletes,
for the cost in deletecost. */
char deletecount;
};
/* This exceeds the sum of any reasonable number of INFINITY's. */
#define SUPER_INFINITY (1000 * INFINITY)
/* See CalcIDCosts for on the arrays below */
int *ILcost;
int *DLcost;
int *ILncost;
int *DLncost;
scrolling_1 (window_size, unchanged_at_top, unchanged_at_bottom,
draw_cost, old_hash, new_hash, free_at_end)
int window_size, unchanged_at_top, unchanged_at_bottom;
int *draw_cost;
int *old_hash;
int *new_hash;
int free_at_end;
{
struct matrix_elt *matrix;
matrix = ((struct matrix_elt *)
alloca ((window_size + 1) * (window_size + 1) * sizeof *matrix));
calculate_scrolling (matrix, window_size, unchanged_at_bottom,
draw_cost, old_hash, new_hash,
free_at_end);
do_scrolling (matrix, window_size, unchanged_at_top);
}
/* Determine, in matrix[i,j], the cost of updating the first j old lines
into the first i new lines.
This involves using insert or delete somewhere if i != j.
For each matrix elements, three kinds of costs are recorded:
the smallest cost that ends with an insert, the smallest
cost that ends with a delete, and the smallest cost that
ends with neither one. These are kept separate because
on some terminals the cost of doing an insert varies
depending on whether one was just done, etc. */
/* draw_cost[VPOS] is the cost of outputting new line at VPOS.
old_hash[VPOS] is the hash code of the old line at VPOS.
new_hash[VPOS] is the hash code of the new line at VPOS.
Note that these are not true screen vpos's, but relative
to the place at which the first mismatch between old and
new contents appears. */
calculate_scrolling (matrix, window_size, lines_below,
draw_cost, old_hash, new_hash,
free_at_end)
/* matrix is of size window_size + 1 on each side. */
struct matrix_elt *matrix;
int window_size;
int *draw_cost;
int *old_hash;
int *new_hash;
int free_at_end;
{
register int i, j;
register struct matrix_elt *p, *p1;
register int cost, cost1;
int lines_moved = window_size + (scroll_region_ok ? 0 : lines_below);
/* We subtract 1 to compensate for the fact that i and j have values
starting with 1. */
int *first_insert_cost = &ILcost[screen_height - lines_moved - 1];
int *first_delete_cost = &DLcost[screen_height - lines_moved - 1];
int *next_insert_cost = &ILncost[screen_height - lines_moved - 1];
int *next_delete_cost = &DLncost[screen_height - lines_moved - 1];
/* initialize the top left corner of the matrix */
matrix->writecost = 0;
matrix->insertcost = SUPER_INFINITY;
matrix->deletecost = SUPER_INFINITY;
matrix->insertcount = 0;
matrix->deletecount = 0;
/* initialize the left edge of the matrix */
cost = first_insert_cost[1] - next_insert_cost[1];
for (i = 1; i <= window_size; i++)
{
p = matrix + i * (window_size + 1);
cost += draw_cost[i] + next_insert_cost[i];
p->insertcost = cost;
p->writecost = SUPER_INFINITY;
p->deletecost = SUPER_INFINITY;
p->insertcount = i;
p->deletecount = 0;
}
/* initialize the top edge of the matrix */
cost = first_delete_cost[1] - next_delete_cost[1];
for (j = 1; j <= window_size; j++)
{
cost += next_delete_cost[j];
matrix[j].deletecost = cost;
matrix[j].writecost = SUPER_INFINITY;
matrix[j].insertcost = SUPER_INFINITY;
matrix[j].deletecount = j;
matrix[j].insertcount = 0;
}
/* `i' represents the vpos among new screen contents.
`j' represents the vpos among the old screen contents. */
p = matrix + window_size + 2; /* matrix [1, 1] */
for (i = 1; i <= window_size; i++, p++)
for (j = 1; j <= window_size; j++, p++)
{
/* p contains the address of matrix [i, j] */
/* First calculate the cost assuming we do
not insert or delete above this line.
That is, if we update through line i-1
based on old lines through j-1,
and then just change old line j to new line i. */
p1 = p - window_size - 2; /* matrix [i-1, j-1] */
cost = p1->writecost;
if (cost > p1->insertcost)
cost = p1->insertcost;
if (cost > p1->deletecost)
cost = p1->deletecost;
if (old_hash[j] != new_hash[i])
cost += draw_cost[i];
p->writecost = cost;
/* Calculate the cost if we do an insert-line
before outputting this line.
That is, we update through line i-1
based on old lines through j,
do an insert-line on line i,
and then output line i from scratch,
leaving old lines starting from j for reuse below. */
p1 = p - window_size - 1; /* matrix [i-1, j] */
/* No need to think about doing a delete followed
immediately by an insert. It cannot be as good
as not doing either of them. */
if (free_at_end == i)
{
cost = p1->writecost;
cost1 = p1->insertcost;
}
else
{
cost = p1->writecost + first_insert_cost[i];
if (p1->insertcount > i)
abort ();
cost1 = p1->insertcost + next_insert_cost[i - p1->insertcount];
}
p->insertcost = min (cost, cost1) + draw_cost[i];
p->insertcount = (cost < cost1) ? 1 : p1->insertcount + 1;
if (p->insertcount > i)
abort ();
/* Calculate the cost if we do a delete line after
outputting this line.
That is, we update through line i
based on old lines through j-1,
and throw away old line j. */
p1 = p - 1; /* matrix [i, j-1] */
/* No need to think about doing an insert followed
immediately by a delete. */
if (free_at_end == i)
{
cost = p1->writecost;
cost1 = p1->deletecost;
}
else
{
cost = p1->writecost + first_delete_cost[i];
cost1 = p1->deletecost + next_delete_cost[i];
}
p->deletecost = min (cost, cost1);
p->deletecount = (cost < cost1) ? 1 : p1->deletecount + 1;
}
}
/* Perform insert-lines and delete-lines operations
according to the costs in the matrix.
Updates the contents of current_screen to record what was done. */
do_scrolling (matrix, window_size, unchanged_at_top)
struct matrix_elt *matrix;
int window_size;
int unchanged_at_top;
{
register struct matrix_elt *p;
register int i, j;
struct queue { int count, pos; } *queue;
int offset = unchanged_at_top;
int qi = 0;
int window = 0;
register int tem;
int next;
queue = (struct queue *) alloca (screen_height * sizeof (struct queue));
bcopy (current_screen->contents, temp_screen->contents,
current_screen->height * sizeof (char *));
bcopy (current_screen->used, temp_screen->used,
current_screen->height * sizeof (int));
bcopy (current_s